include settings.mk

####################################################################
# rules
####################################################################
.PHONY: all
all:
	@echo '----------------------------------------------------'
	@echo 'botPMA make tool'
	@echo 'version:	0.3'
	@echo 'author:  David Asabina'
	@echo 'date: June 2, 2011'
	@echo ''
	@echo 'use the command "make help" to get a short'
	@echo 'description of the possible build parameters'
	@echo '----------------------------------------------------'
	@make upload

# create all objects files from source
$(PATHOBJ)core_%.o : $(PATHARDUINO)%.c
	@echo 'building arduino object for '$<
	@mkdir -p $(dir $@)
	$(GCC) $(ALL_CORE_CFLAGS) -c $< -o $@
	@echo '-------------------------------------------------->>'

$(PATHOBJ)bot_%.o : $(PATHSRC)%.c
	@echo 'building robot object for '$<
	@mkdir -p $(dir $@)
	$(GCC) $(CFLAGS) -c $< -o $@
	@echo '-------------------------------------------------->>'

# creates all Arduino core objects
.PHONY: core
core: $(SRCARDUINO:%.c=$(PATHOBJ)core_%.o)
	@echo '----------------------------------------------------'
	@echo 'arduino core objects build'
	@echo '--------------------------------------------------||'

# creates all robot objects
.PHONY: bot
bot: $(SRCBOT:%.c=$(PATHOBJ)bot_%.o)
	@echo '----------------------------------------------------'
	@echo 'bot objects build'
	@echo '--------------------------------------------------||'

# misc
.PHONY: help
help:
	@echo '----------------------------------------------------'
	@echo 'HELP'
	@echo 'make help'
	@echo '	- help menu'
	@echo 'make arduinocore'
	@echo '	- builds the core arduino objects'
	@echo 'make clean'
	@echo '	- removes temporary files from projec dir'
	@echo '----------------------------------------------------'

.PHONY: upload
upload: $(PATHBIN)$(TARGET:.elf=.hex)
	@echo '----------------------------------------------------'
	@echo 'shooting the code into the mcu'
	@echo '----------------------------------------------------'
	$(DUDE) $(DUDEFLAGS)
	@echo '--------------------------------------------------//'
	@echo 'all done :)'

.PHONY: clean
clean:
	$(RM) $(PATHOBJ:%/=%)
	$(RM) $(PATHBIN:%/=%)
	$(RM) $(PATHLIB:%/=%)
	$(RM) $(PATHTMP:%/=%)
	$(CLEAR)

.PHONY: emacs-clean
emacs-clean:
	$(RM) $(PATHSRC)#*
	$(RM) #*
	$(RM) $(PATHSRC)*~
	$(RM) *~
	$(CLEAR)

# link all object files
$(PATHBIN)$(TARGET): $(OBJ) $(PATHLIB)core.a
	@echo '----------------------------------------------------'
	@echo 'linking objects to elf'
	@echo '----------------------------------------------------'
	@mkdir -p $(dir $@)
#	removed -lm before $(OBJBOT)
	$(GXX) $(PATHLIB)core.a $(OBJBOT) -o $@
	@echo '--------------------------------------------------||'

$(PATHBIN)$(TARGET:.elf=.hex): $(PATHBIN)$(TARGET)
	@echo '----------------------------------------------------'
	@echo 'creating hex from elf'
	@echo '----------------------------------------------------'
	$(COPY) -O ihex $(HEX_FLASH_FLAGS) $< $@
	@echo '--------------------------------------------------||'

# create core library
$(PATHLIB)core.a: $(OBJCORE)
	@echo '----------------------------------------------------'
	@echo 'creating core library'
	@echo '----------------------------------------------------'
	@mkdir -p $(dir $@)
	$(AR) rcs $(PATHLIB)core.a $(OBJCORE);
	@echo '--------------------------------------------------||'

# create archive of working directory
%.tar.gz:
	@echo '----------------------------------------------------'
	@echo 'creating tar archive $@'
	@echo '----------------------------------------------------'
	tar -zcvf ../$@ ../botPMA
	@echo '--------------------------------------------------||'

$(PATHTMP)%.dump: $(PATHOBJ)%.o
	@echo '----------------------------------------------------'
	@echo 'dumping $< to $@'
	@echo '----------------------------------------------------'
	@mkdir -p $(dir $@)
	$(DUMP) -x -d $< > $@